/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.domain.vo.writehandler;

import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.comac.ins.bomt.common.enums.StrengthChangeStatisticTypeEnum;
import com.comac.ins.bomt.domain.vo.DetailStrengthStandardChangeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghongyi
 * @date 2024/8/19
 * @description StrengthStandardChangeDetailWriteHandler
 */
public class StrengthStandardChangeDetailWriteHandler implements CellWriteHandler {
    private final Map<String, CellStyle> colorCache = new HashMap<>();
    private final List<DetailStrengthStandardChangeVo> detailData;

    public StrengthStandardChangeDetailWriteHandler(List<DetailStrengthStandardChangeVo> data) {
        detailData = data;

    }

    /**
     * 参考 <a href="https://blog.csdn.net/qq_34828100/article/details/122577794">...</a>
     */
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        int rowIndex = cell.getRowIndex();
        int columnIndex = cell.getColumnIndex();

        if (BooleanUtils.isNotTrue(context.getHead())) {
            String changeType = detailData.get(rowIndex - 1).getChangeType();
            String note = detailData.get(rowIndex - 1).getNote();
            //设置图号颜色
            if (columnIndex == 1) {
                // 拿到poi的workbook
                Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                // 这里千万记住 想办法能复用的地方把他缓存起来 一个表格最多创建6W个样式
                // 不同单元格尽量传同一个 cellStyle
                CellStyle changeColor = getChangeColor(workbook, StrengthChangeStatisticTypeEnum.getByType(changeType));
                cell.setCellStyle(changeColor);
                // 由于这里没有指定dataformat 最后展示的数据 格式可能会不太正确
                // 这里要把 WriteCellData的样式清空， 不然后面还有一个拦截器 FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到
                // cell里面去 会导致自己设置的不一样（很关键）
                context.getFirstCellData().setWriteCellStyle(null);
            }
            //设置批注
            if (columnIndex == 1 && StringUtils.isNotBlank(note)) {
                Sheet sheet = context.getWriteSheetHolder().getSheet();
                Drawing<?> draw = sheet.createDrawingPatriarch();
                Comment comment = draw.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, columnIndex, rowIndex, columnIndex + 1, rowIndex + 1));
                comment.setString(new XSSFRichTextString(note));
                cell.setCellComment(comment);
            }

            //设置版本颜色
            if (context.getHeadData().getHeadNameList().contains(detailData.get(rowIndex - 1).getVersion())) {
                // 拿到poi的workbook
                Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                // 这里千万记住 想办法能复用的地方把他缓存起来 一个表格最多创建6W个样式
                // 不同单元格尽量传同一个 cellStyle
                CellStyle versionColor = getVersionColor(workbook, StrengthChangeStatisticTypeEnum.getByType(changeType));
                cell.setCellStyle(versionColor);
                // 由于这里没有指定dataformat 最后展示的数据 格式可能会不太正确
                // 这里要把 WriteCellData的样式清空， 不然后面还有一个拦截器 FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到
                // cell里面去 会导致自己设置的不一样（很关键）
                context.getFirstCellData().setWriteCellStyle(null);
            }
        }
    }

    private CellStyle getChangeColor(Workbook workbook, StrengthChangeStatisticTypeEnum changeType) {
        if (colorCache.containsKey(changeType.getType())) {
            return colorCache.get(changeType.getType());
        }
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        switch (changeType) {
            case ADD:
                cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
                break;
            case NUMBER:
            case CHANGE:
            case CHECKED:
            case SAME:
                cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                break;
            default:
                cellStyle = null;
                break;
        }
        colorCache.put(changeType.getType(), cellStyle);
        return cellStyle;
    }

    private CellStyle getVersionColor(Workbook workbook, StrengthChangeStatisticTypeEnum changeType) {
        String key = "V" + changeType.getType();
        if (colorCache.containsKey(key)) {
            return colorCache.get(key);
        }
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        switch (changeType) {
            case NUMBER:
                cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                break;
            case CHANGE:
                cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
                break;
            case ADD:
            case CHECKED:
            case SAME:
            default:
                cellStyle = null;
                break;
        }
        colorCache.put(key, cellStyle);
        return cellStyle;
    }
}
